package com.qf.servlet;

import com.qf.entity.Book;
import com.qf.service.BookService;
import com.qf.service.serviceImpl.BookServiceimpl;
import com.qf.util.Myutil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "AddBookServlet", urlPatterns = "/addBook")
public class AddBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        //接收参数
        String name = request.getParameter("name");
        String price1 = request.getParameter("price");
        Double price = Double.parseDouble(price1);
        String description = request.getParameter("description");
        String author = request.getParameter("author");
        String publisher = request.getParameter("publisher");


//        System.out.println(name);
//        System.out.println(price);
//        System.out.println(description);
//        System.out.println(author);
//        System.out.println(publisher);

        //判断内容是否为空
        if (Myutil.isEmpty(name)) {
            session.setAttribute("msg", "书名不能为空！");
            //request.getContextPath()   返回当前页面所在的应用的名字;  项目的虚拟路径
            response.sendRedirect(request.getContextPath() + "/addBook.jsp");
            return;
        }
//        if (Myutil.isEmpty(price1)) {
//            session.setAttribute("msg", "价格不能为空！");
//            response.sendRedirect(request.getContextPath() + "/addBook.jsp");
//            return;
//        }
//        if (Myutil.isEmpty(description)) {
//            session.setAttribute("msg", "简介不能为空！");
//            //request.getContextPath()   返回当前页面所在的应用的名字;  项目的虚拟路径
//            response.sendRedirect(request.getContextPath() + "/addBook.jsp");
//            return;
//        }
        if (Myutil.isEmpty(author)) {
            session.setAttribute("msg", "作者不能为空！");
            //request.getContextPath()   返回当前页面所在的应用的名字;  项目的虚拟路径
            response.sendRedirect(request.getContextPath() + "/addBook.jsp");
            return;
        }
//        if (Myutil.isEmpty(publisher)) {
//            session.setAttribute("msg", "出版社不能为空！");
//            //request.getContextPath()   返回当前页面所在的应用的名字;  项目的虚拟路径
//            response.sendRedirect(request.getContextPath() + "/addBook.jsp");
//            return;
//        }


        Book book = new Book(name, price, description, author, publisher);
        BookService bookService = new BookServiceimpl();
        Boolean result = bookService.addBook(book);
        if (result) {
            response.sendRedirect(request.getContextPath() + "/bookList");
        } else {
            session.setAttribute("msg", "添加失败");
            response.sendRedirect(request.getContextPath() + "/addBook.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
